package com.dxyxx.framework;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.codehaus.jackson.type.JavaType;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.customized.ExpandView;
import com.dxyxx.R;
import com.dxyxx.framework.CommentFragment.ItemClickListener;
import com.dxyxx.framework.CommentFragment.ReplyClickListener;
import com.dxyxx.framework.CommentFragment.ViewClickListener;
import com.dxyxx.framework.CommentFragment.ViewHolder;
import com.dxyxx.pojo.CommentReply;
import com.dxyxx.pojo.ResponseResult;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.ta.utdid2.android.utils.StringUtils;
import com.weixinninegridlayout.CustomImageView;
import com.weixinninegridlayout.Image;
import com.weixinninegridlayout.NineGridlayout;
import com.weixinninegridlayout.ScreenTools;

public class CommentDetailsActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_commentdetails);

		initViews();
	}

	private final static String FROM = "from";
	private final static String TIME = "time";
	private final static String IMAGE = "image";
	private final static String WORD = "word";
	private final static String ID = "id";
	private final static String REPLYS = "replys";
	private final static String CID = "cid";
	private final static String THUMB = "thumb";
	private final static String USERNAME = "userName";

	private void initViews() {
		ImageView back = (ImageView) findViewById(R.id.backImageButton);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();

			}
		});
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText("评论详情");
		titleView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		findViewById(R.id.nextImageButton).setVisibility(View.GONE);

		///---
		NineGridlayout ivMore = (NineGridlayout) findViewById(R.id.iv_ngrid_layout);
		CustomImageView ivOne = (CustomImageView) findViewById(R.id.iv_oneimage);
		TextView wordText = (TextView) findViewById(R.id.text);
		TextView timeText = (TextView) findViewById(R.id.time);
		TextView nameText = (TextView) findViewById(R.id.name);
		ImageView thumbView = (ImageView) findViewById(R.id.thumbImage);
		ImageView replyView = (ImageView) findViewById(R.id.replyIcon);
		replyView.setVisibility(View.GONE);
		TextView openORclose = (TextView) findViewById(R.id.openORclose);
		ExpandView expandView = (ExpandView) findViewById(R.id.expandView);
		Bundle bundle = getIntent().getExtras();
		List<Image> itemList = new ArrayList<Image>();
		String[] images = bundle.getString(IMAGE).split(";");
		for (String str : images) {

			String[] info = str.split("_");
			itemList.add(new Image(baseImageURL + info[0] + "?p=0", Integer.parseInt(info[1]), Integer.parseInt(info[2])));
		}
		// 设置监听器。
		ItemClickListener icl = new ItemClickListener(bundle.getString(IMAGE));
		if (itemList.size() == 0) {
			ivMore.setVisibility(View.GONE);
			ivOne.setVisibility(View.GONE);
		} else if (itemList.size() == 1) {
			ivMore.setVisibility(View.GONE);
			ivOne.setVisibility(View.VISIBLE);
			ivOne.setOnClickListener(icl);

			handlerOneImage(ivOne, itemList.get(0));
		} else {
			ivMore.setVisibility(View.VISIBLE);
			ivOne.setVisibility(View.GONE);
			ivMore.setImagesData(bundle.getString(IMAGE), itemList);
			//				ivMore.setOnClickListener(icl);// 这句话不用了
		}

		wordText.setText(bundle.getString(WORD));
		timeText.setText(bundle.getString(TIME));
		nameText.setText(bundle.getString(USERNAME));
		if (bundle.getString(THUMB) != null) {
			imageLoader.displayImage(bundle.getString(THUMB), thumbView, squareOptions, animateFirstListener);
		}

		// 点击收缩。

		openORclose.setOnClickListener(new ExpandClickListener(expandView, bundle.getString(CID)));
		if (StringUtils.isEmpty(bundle.getString(REPLYS))) {
			openORclose.setText("回复(0)");
		} else {
			int num = bundle.getString(REPLYS).split(";").length;
			openORclose.setText("回复(" + num + ")");
		}
		// 点击回复
		//replyView.setOnClickListener(new ReplyClickListener(bundle.getString(CID)));
	}

	private void handlerOneImage(CustomImageView ivOne, Image image) {
		int totalWidth;
		int imageWidth;
		int imageHeight;
		ScreenTools screentools = ScreenTools.instance(CommentDetailsActivity.this);
		totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
		imageWidth = screentools.dip2px(image.getWidth());
		imageHeight = screentools.dip2px(image.getHeight());
		if (image.getWidth() <= image.getHeight()) {
			if (imageHeight > totalWidth) {
				imageHeight = totalWidth;
				imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
			}
		} else {
			if (imageWidth > totalWidth) {
				imageWidth = totalWidth;
				imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
			}
		}
		ViewGroup.LayoutParams layoutparams = ivOne.getLayoutParams();
		layoutparams.height = imageHeight;
		layoutparams.width = imageWidth;
		ivOne.setLayoutParams(layoutparams);
		ivOne.setClickable(true);
		ivOne.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP);
		//imageLoader.displayImage(image.getUrl(), ivOne, squareOptions, animateFirstListener);
		ivOne.setImageUrl(image.getUrl());

	}

	class ItemClickListener implements OnClickListener {
		private String images;

		public ItemClickListener(String images) {
			this.images = images;
		}

		@Override
		public void onClick(View arg0) {
			Intent intent = new Intent(CommentDetailsActivity.this, ViewPagerExampleActivity.class);
			intent.putExtra("images", images);
			startActivity(intent);
		}

	}

	class ExpandClickListener implements OnClickListener {
		private ExpandView mExpandView;
		private String cid;

		public ExpandClickListener(ExpandView mExpandView, String cid) {
			this.mExpandView = mExpandView;
			this.cid = cid;
		}

		@Override
		public void onClick(View arg0) {
			if (mExpandView.isExpand()) {
				mExpandView.collapse();
			} else {
				//  http 请求， 然后就是 setContent，展开。
				String url = "comment/getReplys?commentId=" + cid;

				HttpUtil.get(url, new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
						try {
							JavaType type1 = getObjectMapper().getTypeFactory().constructCollectionType(List.class, CommentReply.class);
							JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(ResponseResult.class, type1);
							ResponseResult result = getObjectMapper().readValue(arg2, javaType);
							mExpandView.setContentView(baseImageURL, imageLoader, (List<CommentReply>) result.getObject());
							mExpandView.expand();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
						String e = "";
					}
				});

			}
		}

	}

}
